package com.roadjava.rbac.controller;

import com.roadjava.rbac.bean.entity.BannerRes;
import com.roadjava.rbac.bean.req.resource.AddBannerResParam;
import com.roadjava.rbac.bean.res.Result;
import com.roadjava.rbac.bean.vo.BannerResVo;
import com.roadjava.rbac.service.BannerResService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 推荐图片上传
 */
@RestController
@RequestMapping("/bannerRes")
public class BannerController {
    @Resource
    BannerResService bannerResService;
    @Value("${upload.resUrl}")
    private String resUrl;
    @PostMapping("/addRes")
    public Result<String> addRes(@RequestBody AddBannerResParam param) {
        String message = bannerResService.addRes(param);
        return Result.buildSuccess(message);

    }


    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            String message = bannerResService.uploadFile(file);
            return Result.buildSuccess(message);
        } catch (IOException e) {
            return Result.buildFailure("上传失败");
        }
    }

    // 获取文件列表
    @GetMapping("/list")
    public Result<List<BannerResVo>> getFileList() {
        List<BannerRes> fileList = bannerResService.getAllFiles();
        List<BannerResVo> res = new ArrayList<>();
        for (BannerRes banRes : fileList) {
            BannerResVo bannerResVo = new BannerResVo();
            BeanUtils.copyProperties(banRes, bannerResVo);
            bannerResVo.setUrl(resUrl+"/"+bannerResVo.getPath());
            res.add(bannerResVo);
        }
        return Result.buildSuccess(res);
    }

    //根据类型获取文件列表
    @GetMapping("/getImgByType")
    public Result<List<BannerResVo>> getImgByType(@RequestParam("type") Integer type) {
        List<BannerRes> fileList = bannerResService.getImgByType(type);
        List<BannerResVo> res = new ArrayList<>();
        for (BannerRes banRes : fileList) {
            BannerResVo bannerResVo = new BannerResVo();
            BeanUtils.copyProperties(banRes, bannerResVo);
            bannerResVo.setUrl(resUrl+"/"+bannerResVo.getPath());
            res.add(bannerResVo);
        }
        return Result.buildSuccess(res);
    }

    @GetMapping("/banners")
    public Result<List<String>> banners() {
        List<BannerRes> fileList = bannerResService.getAllFiles();
        List<String> res = new ArrayList<>();
        for (BannerRes bannerRes : fileList) {

            res.add(resUrl+"/"+bannerRes.getPath());
        }
        return Result.buildSuccess(res);
    }


    // 删除文件接口
    @GetMapping("/delete/{id}")
    public Result<String> deleteFile(@PathVariable("id") Integer id) {
        String message = bannerResService.deleteFile(id);
        return Result.buildSuccess(message);

    }
}
